package com.jzit168.module_main.ui.main

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter

/**
 *@Desc: 切换ViewPager2加载的Fragment
 *@Author: Android Developer
 *@Date: 2020/10/20
 *
 **/

class SwitchAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
    FragmentStateAdapter(fragmentManager, lifecycle) {

    private val fragments = mutableListOf<Fragment>()

    override fun getItemCount(): Int = fragments.size

    override fun createFragment(position: Int): Fragment = fragments[position]

    fun addFragment(fragment: Fragment) {
        if (!fragments.contains(fragment)) {
            fragments.add(fragment)
        }
    }

}